var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "tagId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl",
            name:"tagName"
        }
    }
};
var ztree;
$(function () {
    $(window).on('resize', function () {
        var editorSelector = $('#editor .editorText');
        editorSelector.height($(this).height-50);
    }).resize();
    $("#jqGrid").jqGrid({
        url: basePath + '/learningExperience/list',
        datatype: "json",
        colModel: [
            { label: '编号', name: 'experienceId', index: 'experienceId',hidden:true,  width: 20,key: true},
            { label: '标题', name: 'experienceTitle', index: 'experienceTitle',  width: 20 },
            { label: '分类', name: 'className', index: 'className',  width: 15 },
            { label: '标签', name: 'tagName', index: 'tagName',  width: 15 },
            { label: '关键字', name: 'keyWord', index: 'keyWord',  width: 20 },
            { label: '创建时间', name: 'createTime', index: 'createTime',  width: 20 },
            { label: '创建人', name: 'userName', index: 'userName',  width: 10 }
        ],
        viewrecords: true,
        height: 'auto',
        rowNum: 15,
        rowList : [15,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

});


var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            name: null,
            classId: null,
            tagId:null,
            tagName:null
        },
        options: [],
        showTypeOptions:[],
        showList: true,
        title: null,
        dict: {
            experienceId:null,
            experienceTitle:null,
            experienceText:null,
            experienceContent:null,
            classId:null,
            tagId:0,
            keyWord:null,
            showType:1,
            tagName:null,
            orderNumber:0
        },
        editor:null
    },
    methods: {
        getTagTree: function(){
            //加载标签树
            $.get(basePath + "/resourceTag/select", function(r){
                ztree = $.fn.zTree.init($("#tagTree"), setting, r.tagList);
                var node = ztree.getNodeByParam("tagId", vm.dict.tagId);
                ztree.selectNode(node);

                vm.dict.tagName = node.tagName;
            })
        },
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.dict = {
                experienceTitle:null,
                experienceText:null,
                experienceContent:null,
                classId:null,
                tagId:0,
                keyWord:null,
                showType:1,
                tagName:null,
                orderNumber:0
            };
            vm.editor.txt.clear();
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            if(vm.validator()){
                return ;
            }
            var url = vm.dict.experienceId == null ? "/learningExperience/save" : "/learningExperience/update";
            $.ajax({
                type: "POST",
                url: basePath + url,
                contentType: "application/json",
                data: JSON.stringify(vm.dict),
                success: function(r){
                    if(r.code === 200){
                        alert('操作成功', function(index){
                            //vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: basePath + "/learningExperience/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 200){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(basePath + "/learningExperience/info/"+id, function(r){
                vm.dict = r.dict;
                if(vm.dict.experienceContent){
                    vm.editor.txt.html(vm.dict.experienceContent);
                }
                vm.getTagTree();
            });
        },
        getClasslist:function () {
            $.get(basePath + "/resourceClass/list/2", function(r){
                vm.options = r.classList;
                vm.q.classId=-1;
            });
        },
        getShowTypeList:function () {
            $.get(basePath + "/resourceShowType/list", function(r){
                vm.showTypeOptions = r.showTypeList;
            });
        },
        tagTree: function(type){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择标签",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#tagLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    if(type==2){
                        //选择上级菜单
                        vm.dict.tagId = node[0].tagId;
                        vm.dict.tagName = node[0].tagName;
                    }
                    else{
                        vm.q.tagId = node[0].tagId;
                        vm.q.tagName = node[0].tagName;
                    }

                    layer.close(index);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'title': vm.q.name,"classId":vm.q.classId,"tagId":vm.q.tagId},
                page:page
            }).trigger("reloadGrid");
        },
        validator:function () {
            if(isBlank(vm.dict.experienceTitle)){
                alert("标题不能为空");
                return true;
            }
            if(vm.dict.tagId === 0 || isBlank(vm.dict.tagId)){
                alert("请选择标签");
                return true;
            }

            if(vm.dict.classId === -1 || isBlank(vm.dict.classId)){
                alert("请选择分类");
                return true;
            }

            if(isBlank(vm.dict.showType)){
                alert("请选择可见级别");
                return true;
            }
            if(isBlank(vm.dict.keyWord)){
                alert("请填写关键字");
                return true;
            }
        }
    },
    created:function () {
        this.getClasslist();
        this.getShowTypeList();
        this.getTagTree();
    },
    mounted:function () {
        this.editor = new wangEditor( this.$refs.toolbar,this.$refs.editor );
        this.editor.customConfig.onchange = function (html) {
            vm.dict.experienceContent = html;
            vm.dict.experienceText = vm.editor.txt.text().substring(0,155);
        };
        this.editor.customConfig.uploadImgShowBase64 = true;
        this.editor.create();
    }
});